function getDocRect (wnd) {
    wnd = wnd || window;
    doc = wnd.document;
    return { 
        left :doc.documentElement.scrollLeft||doc.body.scrollLeft||wnd.pageXOffset||wnd.scrollX||0,
        top :doc.documentElement.scrollTop||doc.body.scrollTop||wnd.pageYOffset||wnd.scrollY||0,
        width :doc.documentElement.clientWidth||doc.body.clientWidth||wnd.innerWidth||doc.width||0,
        height :doc.documentElement.clientHeight||doc.body.clientHeight||wnd.innerHeight||doc.height||0
    }
}

function XWindow(arg) {
}

XWindow.prototype = {
    type : null,
    msg : "",
    icon : null,
    systip : null,
    width : null,
    height : null,
    
    end_close : false,
    timeout_close : false,
    
    bgdiv : null,
    infodiv : null,
    divId : "xwindow",
    
    buttonsHTML : "",
    actions : {
        yesHandler : null,
        noHandler : null
    }, 
    destroy : null,
    
    init : function(msg, systip, type, width, height, actions){
        this.msg = msg;
        this.type = type;
        this.systip = systip;
        this.width = width;
        this.height = height;
        if(!systip) {
            try {
                this.systip = '系统提示';
            } catch(e) {
                this.systip = '系统提示';
            }
        } 
        if(!width){
            this.width = document.body.clientWidth*0.25; 
            if(this.width<250){
                this.width=250;
            }
        }
        
        if(!height){
            this.height=document.body.clientHeight*0.25; 
            if(this.height<150){
                this.height=150; 
            }
        }
        
        if(type==PigossMsg.TYPE_LOADING) {
            this.width = 100;
            this.height = 40;
        }
        
        this.icon = PigossMsg.ICON_ALERT;
        if(type==PigossMsg.TYPE_CONFIRM) {
            this.icon = PigossMsg.ICON_CONFIRM;
        } else if(type==PigossMsg.TYPE_WARNING) {
            this.icon = PigossMsg.ICON_WARNING;
        } else if(type==PigossMsg.TYPE_ERROR) {
            this.icon = PigossMsg.ICON_ERROR;        
        } else if(type==PigossMsg.TYPE_LOADING) {
            this.icon = PigossMsg.ICON_LOADING;        
        }
        if(type==PigossMsg.TYPE_SHOW || type == PigossMsg.TYPE_MENU_WIN) {
            this.divId += '-show';
        } else if(type==PigossMsg.TYPE_RESULT) {
            this.divId += '-result';
        } else if(type==PigossMsg.TYPE_HELP) {
            this.divId += '-help';
        }
        if(actions) {
	        if(typeof(actions)=="string") {
                this.buttonsHTML = actions;
	        } else {
	            for(p in actions){
	                this[p] = actions[p];
	            }
	        }
        }
    },
    
    createBgDiv : function() {
        this.bgdiv = document.createElement("div");   
        this.bgdiv.style.position = "absolute";   
        this.bgdiv.style.width = "100%";   
        this.bgdiv.style.height = "100%";   
        this.bgdiv.style.top="0";   
        this.bgdiv.style.left="0";   
        this.bgdiv.style.background = "#CCCCCC";  
        this.bgdiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=50)"; 
        this.bgdiv.style.opacity = "0.6"; 
        this.bgdiv.style.zIndex = "1112"; 
        this.bgdiv.style.top = 0;   
        this.bgdiv.style.left = 0;    
        document.body.appendChild(this.bgdiv);
    },
    
    /* private */
    getLoadingDivHTML : function() {
        var strHtml = "";
        strHtml =  "<div style='width:100%;float:left; height:100%;border:2px solid;' class='border_color_common' class='bg_color_common'>"  
        strHtml += " <div style='border:2px solid;margin:2px 2px 2px 2px;background:#fff;padding:6px 0px 6px 0px;' class='border_color_common'>"  
        strHtml += " <img src='"+this.icon+"' style='vertical-align:middle;'> " + this.msg  
        strHtml += " </div>"  
        strHtml += "</div>"
        return strHtml;
    },
    
    /* private */
    getMsgboxDivHTML : function(isAlert) {
        var strHtml = "";
        var imgStr = "";
        if( this.icon!="" ) {
           imgStr = "<img src='" + this.icon + "'>";
        }
        strHtml =  "<div class='border_color_common bg_color_common' style='overflow:hidden;width:100%;float:left;height:100%;border:1px solid;}'>" 
        strHtml += "<div style='overflow:visible;cursor:move; width:100%;float:left; height:'25px';' onmousedown='oMove(parentNode.parentNode);'>" 
        strHtml += "    <div class='ptitle' >" + this.systip + " </div>"  
        strHtml += "    <div class='pbutton'><a href=\"javascript:imgClose();\"><div class='pclose'></div></a></div>"
        strHtml += "</div>" 
      
        strHtml += "<div style='overflow:visible;width:100%;height:"+(this.height-90)+"px;margin:10px 15px 10px 15px;vertical-align:middle;' class='bg_color_common'>"
        strHtml += "<table width=100% height=100%><tr>" //<td width=30% align=center>"+imgStr+"</td>"
        strHtml += "<td align=center style='font-size:14px'>" + this.msg + "</td>" 
        strHtml += "</tr></table></div>"  
        strHtml += "<center>"  
        if(isAlert) {
            strHtml += "<input type=button value=' " + PigossMsg.BUTTON_OK + " ' style='width:"+this.width/4+"; height:"+this.height/6+"' onclick='yesclick()' >"
        } else {
            strHtml += "<table width='80%'><tr>"
            strHtml += "<td width='50%' align='center'><input type=button value=' " + PigossMsg.BUTTON_YES + " ' style='width:"+this.width/4+"; height:"+this.height/6+"'  onclick='yesclick()' ></td>"
            strHtml += "<td width='50%' align='center'><input type=button value=' " + PigossMsg.BUTTON_NO + " ' style='width:"+this.width/4+"; height:"+this.height/6+"'  onclick='noclick()' ></td>"
            strHtml += "</tr></table>"
        }   
        strHtml += "</div>"
        return strHtml;
    },
    
    getMenuDivHTML : function(){
    	var strHtml = "<div class='bg_color_window border_color_common' style='width:"+this.width+"px;overflow:hidden;float:left;border:1px solid;}'>";
    	
    	var padding = "";
    	
    	var toolbarHeight = 30;
        var mar = 2;
        if(this.buttonsHTML=='') {
            toolbarHeight = 4;
            mar = 5;
        }
        var imgCloseFun = "imgCloseShow()";
        
        strHtml += "<div class='x-toolbar' style='cursor:move;width:100%;border-top:0px;' onmousedown='oMove(parentNode.parentNode);'>";
        strHtml += "    <div class='ptitle'>"+this.systip+"</div>"
        strHtml += "    <div class='pbutton'><a href='javascript:"+imgCloseFun+";'><div class='pclose'></div></a></div>"
        strHtml += "</div>";
        
    	
    	strHtml += "<div id='xwindow-show-content' class='border_color_common' style='overflow:hidden;height:"+(this.height-31-toolbarHeight)+"px;background:#fff;' >"
		strHtml += this.msg  
		strHtml += "</div>"
    	
    	strHtml += "<div>";
    	return strHtml;
    },
    
    getShowDivHTML: function() {
        var strHtml = "";
        var padding = "";
        
        var imgCloseFun = "imgCloseShow()";
        if(this.type==PigossMsg.TYPE_RESULT) {
            imgCloseFun = "imgCloseResult()";
        } else if(this.type==PigossMsg.TYPE_HELP) {
            imgCloseFun = "imgCloseHelp()";
            padding = "padding:5px";
        } 
        var toolbarHeight = 30;
        var mar = 2;
        if(this.buttonsHTML=='') {
            toolbarHeight = 4;
            mar = 5;
        }
		strHtml="<div class='bg_color_window border_color_common' style='width:"+this.width+"px;overflow:hidden;float:left;border:1px solid;}'>"
		strHtml += " <div style='cursor:move;width:100%' onmousedown='oMove(parentNode.parentNode);' > "  
		strHtml += "    <div class='ptitle'>"+this.systip+"</div>"
		strHtml += "    <div class='pbutton'><a href='javascript:"+imgCloseFun+";'><div class='pclose'></div></a></div>"
		strHtml += "</div>" 
		
		strHtml += "<div id='xwindow-show-content' class='border_color_common' style='overflow:hidden;height:"+(this.height-31-toolbarHeight)+"px;background:#fff;margin:5px 5px "+mar+"px 5px;"+ padding +"border:2px solid; ' >"
		strHtml += this.msg  
		strHtml += "</div>"
      
    /*    strHtml += "<iframe src='"+ this.msg+"' style='width:100%;height:"+(this.height-65)+"px;background:#fff;margin:5px 5px 2px 5px;border:1px;' >"
        strHtml += "</iframe>"
      */
        if(this.buttonsHTML!='') {
			strHtml += "<div class='toolbar' style='margin:0px 5px 1px 5px;'>"   
			strHtml += "<img src='/netistate-pms/images/common/shux.gif' class='split'>"
			if(this.buttonsHTML.substr(0,5)=='fun::') {
			    var func = this.buttonsHTML.substr(5);
			    if(func.indexOf("<")>=0) {
			         alert("PigossMsg.show's actions maybe invalid, Please check the src!");
			    }
                strHtml += "<a href=\"javascript:"+func+";\" class='buttonlink'><img src='/netistate-pms/images/common/save.gif'><p>确定</p></a>" 
                strHtml += "<img src='/netistate-pms/images/common/shux.gif' class='split'>"
               
			} else { 
	           strHtml += this.buttonsHTML
	        } 
	        strHtml += "<a href='javascript:"+imgCloseFun+";' class='buttonlink'><img src='/netistate-pms/images/common/close.png'><p>取消</p></a>"  
			strHtml += "<img src='/netistate-pms/images/common/shux.gif' class='split'>"  
			strHtml += "</div>"
		}     
		strHtml += "</div>"  
        return strHtml;
    },

    createMsgboxDiv : function() {
        this.infodiv = document.createElement("div");
        this.infodiv.id = this.divId;
        this.infodiv.setAttribute('id', this.divId);   
        this.infodiv.style.position = "absolute";  
        this.infodiv.style.width = this.width + "px";   
        this.infodiv.style.height = this.height + "px";
           
        this.infodiv.style.top = parseInt((document.body.clientHeight - parseInt(this.height)) / 2) + "px"; 
        this.infodiv.style.left = parseInt((document.body.clientWidth - parseInt(this.width)) / 2) + "px";
        if(this.type!=PigossMsg.TYPE_MENU_WIN){
        	this.infodiv.style.background = "#CCCCCC";
        }
        this.infodiv.style.opacity = "1"; 
        this.infodiv.style.zIndex = "1112"; 
        
        var createOnfous = false;
        if(this.type==PigossMsg.TYPE_LOADING) {
            this.infodiv.innerHTML = this.getLoadingDivHTML();   
        } else if(this.type==PigossMsg.TYPE_SHOW||this.type==PigossMsg.TYPE_RESULT||this.type==PigossMsg.TYPE_HELP){
            this.infodiv.innerHTML = this.getShowDivHTML(); 
            createOnfous = true;
        } else if(this.type==PigossMsg.TYPE_MENU_WIN){
        	createOnfous = true;
        	this.infodiv.innerHTML = this.getMenuDivHTML(); 
        }else {
            var isAlert = (this.type==PigossMsg.TYPE_CONFIRM)?false:true;
            this.infodiv.innerHTML = this.getMsgboxDivHTML(isAlert);
        } 
       iframe =  this.createIFrame(createOnfous);
        document.body.appendChild(this.infodiv);
        // exit by 'Esx'
        var self = this;
        if(createOnfous){
	        setTimeout(function(){
	        	self.infodiv.focus();
	        },300);
        }
        this.infodiv.onkeydown = function(e){
    		var keycode = event.keyCode;
	       	if(keycode == 27){
	       		imgCloseShow();
	       	}
    	}
    },
    
    createIFrame:function(createOnfous){
    	var iframe = document.createElement("iframe");
    	iframe.style.zIndex = "-1";
    	iframe.style.position = "absolute";   
        iframe.style.width = "100%";   
        iframe.style.height = "100%";   
        iframe.style.top="0";   
        iframe.style.left="0";  
        iframe.style.background = "#CCCCCC";  
        iframe.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=30)"; 
        iframe.style.opacity = "0.6"; 
        iframe.style.zIndex = "1112"; 
    	this.bgdiv.appendChild(iframe);
    	var self = this;
    	
    	this.destroy = function(){
	    		iframe.onfocus = null;
	    	}
    	if(createOnfous){
	    	iframe.onfocus = function(){
	    		if(self.infodiv && self.infodiv.onkeydown){
	    			self.infodiv.focus();
	    		}
	    	}
    	}
    	
    	return iframe;
    },

    removeAll : function() {
        try {
            if(this.destroy) {
               this.destroy();
            }

	        if(this.bgdiv) {
	            document.body.removeChild(this.bgdiv);
	        }
	        if (this.infodiv) {
	            document.body.removeChild(this.infodiv);
	        }
        }catch(e){}
    },
    
    loadingEnd : function() {
        this.end_close = true;
        if(this.timeout_close) {
            this.removeAll();
        }
    },
          
    popup : function() {
        if( $(this.divId)) {
            return;
        } 
        var self = this;

        this.createBgDiv();
        this.createMsgboxDiv();
        
        if(this.type==PigossMsg.TYPE_SHOW||this.type==PigossMsg.TYPE_MENU_WIN) {
            imgCloseShow = function () {
                self.removeAll();
                self.infodiv.onkeydown = null;
                self.infodiv.onblur = null;
            }
        } else if(this.type==PigossMsg.TYPE_RESULT) {
            imgCloseResult = function () {
                self.removeAll();
            }
        } else if(this.type==PigossMsg.TYPE_HELP) {
            imgCloseHelp = function () {
                self.removeAll();
            }
        } else if(this.type==PigossMsg.TYPE_LOADING) {
            remove = function() {
                self.timeout_close = true;
                if(self.end_close) {
                    self.removeAll();
                }
            }
            
            remove9 = function() {
                self.removeAll();
            }

            setTimeout("remove()", 300);   
            setTimeout("remove9()", 10000);   
        } else {
	        imgClose = function() {
	            self.removeAll();
	        }
	        yesclick = function (){
	            if(self.yesHandler){
	                self.yesHandler();
	            }
	            this.imgClose();   
	        }   
	        noclick = function (){   
	           if(self.noHandler){
	               self.noHandler();
	           }
	           this.imgClose();   
	        } 
	    }  
           
        oMove = function(obj) {   
            var otop,oleft;   
            otop = event.y - obj.offsetTop;   
            oleft = event.x - obj.offsetLeft;   
            obj.setCapture();   
            var rect = getDocRect();
      
            obj.onmousemove  = function() {   
                obj.style.left = event.x - oleft;   
                obj.style.top = event.y - otop;
    
                var left = parseInt(obj.style.left);
                var top = parseInt(obj.style.top); 
                var leftMin = 20-parseInt(obj.style.width);
                var leftMax = rect.width -20;
                var topMax  = rect.height-20; 

                if(left<leftMin) obj.style.left = leftMin;
                if(top>topMax) obj.style.top = topMax;
                if(left>leftMax) obj.style.left = leftMax;
                if(top<0) obj.style.top = 0;
            }   
            obj.onmouseup  = function() {   
                obj.onmousemove = null;   
                obj.style.filter = null;   
                obj.releaseCapture();
            }   
        }   
    },
    popupShow : function() {
        var self = this;

        if(this.type!=PigossMsg.TYPE_SHOW && this.type!=PigossMsg.TYPE_MENU_WIN) {
            return;   
        }
        
        this.createBgDiv();
        this.createMsgboxDiv();
        
        imgClose = function () {
            self.removeAll();
        }
    }
}

PigossMsg = {
    TYPE_ALERT : 0,
    TYPE_CONFIRM : 1,
    TYPE_WARNING : 2,
    TYPE_ERROR : 3,
    TYPE_LOADING : 4,
    TYPE_SHOW : 12,
    TYPE_RESULT : 13,
    TYPE_HELP : 14,
    
    TYPE_MENU_WIN : 15,
    
    ICON_ALERT: "/images/window/icon-info.gif",
    ICON_CONFIRM : "/images/window/icon-question.gif",
    ICON_WARNING : "", //"/images/window/icon-warning.gif",
    ICON_ERROR : "/images/window/icon-error.gif",
    ICON_LOADING : "/images/common/loading.gif",
    
    BUTTON_OK : '确定',
    BUTTON_YES : '确定',
    BUTTON_NO : '取消',
    destroy : null,
        
    alert:function(msg,systip,okHandler) {
        var xwin = new XWindow();
        xwin.init(msg, systip, PigossMsg.TYPE_ALERT, null, null,{yesHandler:okHandler});
        xwin.popup();
        return xwin;
    },
    warning:function(msg,systip,okHandler) {
        var xwin = new XWindow();
        xwin.init(msg, systip, PigossMsg.TYPE_WARNING, null, null,{yesHandler:okHandler});
        xwin.popup();
        return xwin;
    },  
    error:function(msg,systip,okHandler) {
        var xwin = new XWindow();
        xwin.init(msg, systip, PigossMsg.TYPE_ERROR, null, null,{yesHandler:okHandler});
        xwin.popup();
        return xwin;
    },  
    loading:function(msg) {
        if(!msg) {
            msg = "Loading";
	        try {
	            msg = "Loading";
	        } catch(e) {
	        }
        }
        var xwin = new XWindow();
        xwin.init(msg, null, PigossMsg.TYPE_LOADING);
        xwin.popup();
        return xwin;
    },  
    confirm:function(msg,systip,yesHandler,noHandler,width,height) {
        var xwin = new XWindow();
        xwin.init(msg, systip, PigossMsg.TYPE_CONFIRM, width, height,{yesHandler:yesHandler,noHandler:noHandler});
        xwin.popup();
        return xwin;
    },  
    show:function(url,systip,width,height,okClick){
        var funClick = "";
        if(okClick!="") {
            funClick = "fun::" + okClick;
        }
        this.showWin(url,systip,width,height,funClick);
    },
    
    showMenuWin:function(url,systip,width,height,buttonsHTML,isEvalScript){
    	if(isEvalScript!=false){
    		isEvalScript = true;
    	}
        var self = this;
        AjaxUtil.request({
            url : url,
            useCache: true,
            success : function(responseText,code){
		        var xwin = new XWindow();
		        xwin.init(responseText, systip, PigossMsg.TYPE_MENU_WIN, width, height, buttonsHTML);
		        xwin.destroy = self.destroy;
		        xwin.popup();
		        if(isEvalScript){
		        	evalScript(responseText);
		        }
                renderTip();
            },
            failure : function() {
                alert("error");
            }
        });
    },
    
    showWin:function(url,systip,width,height,buttonsHTML,isEvalScript){
    	if(isEvalScript!=false){
    		isEvalScript = true;
    	}
        var self = this;
        AjaxUtil.request({
            url : url,
            useCache: true,
            success : function(responseText,code){
		        var xwin = new XWindow();
		        xwin.init(responseText, systip, PigossMsg.TYPE_SHOW, width, height, buttonsHTML);
		        xwin.destroy = self.destroy;
		        xwin.popup();
		        if(isEvalScript){
		        	evalScript(responseText);
		        }	        
                //renderTip();
            },
            failure : function() {
                alert("error");
            }
        });
    },
    showWinWithPost:function(url,params,systip,width,height,buttonsHTML,isEvalScript){
    	if(isEvalScript!=false){
    		isEvalScript = true;
    	}
        var self = this;
        AjaxUtil.request({
            url : url,
            params : params,
            useCache: true,
            success : function(responseText,code){
		        var xwin = new XWindow();
		        xwin.init(responseText, systip, PigossMsg.TYPE_SHOW, width, height, buttonsHTML);
		        xwin.destroy = self.destroy;
		        xwin.popup();
                if(isEvalScript){
		        	evalScript(responseText);
		        }
                renderTip();
            },
            failure : function() {
                alert("error");
            }
        });
    },

    showResult:function(url,systip,width,height,okClick){
        var self = this;
        if(okClick) {
            okClick = "fun::" + okClick;
        }
        AjaxUtil.request({
            url : url,
            success : function(responseText,code){
                var xwin = new XWindow();
                xwin.init(responseText, systip, PigossMsg.TYPE_RESULT, width, height, okClick);
                xwin.destroy = self.destroy;
                xwin.popup();
                evalScript(responseText);
            },
            failure : function() {
                alert("error");
            }
        });
    },
    showHelpWin:function(key,width,height){
        var self = this;
        var url = '/help_getTipForWin.action?key=' + key;
        AjaxUtil.request({
            url : url,
            success : function(responseText,code){
	            var systip = 'Online Help';
	            try {
	                systip = 'Online Help';
	            } catch(e) {
	            }
                var xwin = new XWindow();
                xwin.init(responseText, systip, PigossMsg.TYPE_HELP, width, height);
                xwin.popup();
            },
            failure : function() {
                alert("error");
            }
        });
    }
}